using System.Data;
using System.Text.RegularExpressions;
using FileRead;

namespace Listen;


public class ListenMerge
{
    private readonly List<string> mdDirPaths;

    public ListenMerge(List<string> paths)
    {
        this.mdDirPaths = paths.ToList();
    }
    public void MergeMdFile()
    {
        foreach (var dirPath in this.mdDirPaths)
        {
            var filePath = this.GetListenFilePath(dirPath);
            // 创建一个 txt 文件，如果文件已存在，则覆盖原有内容
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                var dstFilePaths = MarkdownFile.GetDstFilePathByFolder(dirPath);
                foreach (var dstFilePath in dstFilePaths)
                {
                    Console.WriteLine(dstFilePath);
                    if (Path.GetExtension(dstFilePath) == ".md")
                    {
                        using (StreamReader sr = new StreamReader(dstFilePath))
                        {
                            string? line;
                            while ((line = sr.ReadLine()) != null)
                            {
                                if (line == "") continue;
                                if (Regex.IsMatch(Path.GetFileName(line), "^\\#{2} ")) continue;
                                if (Regex.IsMatch(Path.GetFileName(line), "^\\#{1} "))
                                {
                                    sw.WriteLine("[p1500]");
                                    sw.WriteLine(string.Concat("标题是:", line.Substring(2)));
                                    sw.WriteLine("[p500]");
                                }
                                else
                                {
                                    sw.WriteLine(line);
                                }
                            }
                        }
                    }
                    else if (Path.GetExtension(dstFilePath) == ".txt")
                    {
                        using (StreamReader sr = new StreamReader(dstFilePath))
                        {
                            string? line;
                            while ((line = sr.ReadLine()) != null)
                            {

                                if (line == "") continue;
                                if (line == "第一章")
                                {
                                    sw.WriteLine("[p1500]");
                                    sw.WriteLine("标题是:");
                                    continue;
                                };
                                sw.WriteLine(line);
                            }
                        }
                    }
                }


            }

        }

    }



    private string GetListenFilePath(string dirPath)
    {
        // Console.WriteLine(Path.GetFileNameWithoutExtension(dirPath));
        // Console.WriteLine(string.Concat(Path.GetFileNameWithoutExtension(dirPath),".txt"));
        // Console.WriteLine(Path.GetDirectoryName(dirPath));

        // Console.WriteLine(Path.Combine(Path.GetDirectoryName(dirPath)!,"listen",string.Concat(Path.GetFileNameWithoutExtension(dirPath),".txt")));
        // var filePath = Path.GetFileNameWithoutExtension(dirPath);
        // var filePath = Path.Combine(Path.GetDirectoryName(dirPath)!, "listen", string.Concat(Path.GetFileNameWithoutExtension(dirPath), ".txt"));
        var filePath = Path.Combine(Path.GetDirectoryName(dirPath)!, "listen", string.Concat(Path.GetFileNameWithoutExtension(dirPath), ".txt"));
        return filePath;
    }
}